package com.meeting.manager.actionbar;


import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;

import com.meeting.manager.R;

public abstract class InitActionBar implements OnClickListener {
	public Context context;
	public ActionBar actionBar;
	public String mtvBack;
	public String mtvTitle;
	public String mtvSave;
	public int mivSave = -1;
	public TextView tvSave;
	public ImageButton ibtnSave;
	public ImageButton ibtnBack;
	public TextView tvTitle;
	public boolean isShowBack;

	public InitActionBar(Context context, ActionBar actionBar,
			boolean isShowBack, String mtvTitle, String mtvSave) {
		super();
		this.context = context;
		this.actionBar = actionBar;
		this.mtvTitle = mtvTitle;
		this.mtvSave = mtvSave;
		this.isShowBack = isShowBack;
		init();
	}

	public InitActionBar(Context context, ActionBar actionBar,
			boolean isShowBack, String mtvTitle, int mivSave) {
		super();
		this.context = context;
		this.actionBar = actionBar;
		this.mtvTitle = mtvTitle;
		this.mivSave = mivSave;
		this.isShowBack = isShowBack;
		init();
	}

	public InitActionBar(Context context, ActionBar actionBar) {
		this.context = context;
		this.actionBar = actionBar;
	}

	@SuppressLint("NewApi")
	private void init() {
		actionBar.setDisplayHomeAsUpEnabled(false);
		actionBar.setHomeButtonEnabled(true);
		actionBar.setDisplayShowHomeEnabled(false);
		actionBar.setDisplayShowTitleEnabled(false);
		actionBar.setDisplayShowCustomEnabled(true);
		View view = LayoutInflater.from(context).inflate(R.layout.lay_title,
				null);
		tvSave = (TextView) view.findViewById(R.id.tv_save);
		ibtnBack = (ImageButton) view.findViewById(R.id.ibtn_back);
		ibtnSave = (ImageButton) view.findViewById(R.id.ibtn_save);
		tvTitle = (TextView) view.findViewById(R.id.tv_title);
		tvTitle.setText(mtvTitle);
		actionBar.setCustomView(view);
		actionBar.show();
		if (mtvSave != null) {
			tvSave.setVisibility(View.VISIBLE);
			tvSave.setText(mtvSave);
			tvSave.setOnClickListener(this);
		}
		if (mivSave != -1) {
			tvSave.setVisibility(View.GONE);
			ibtnSave.setVisibility(View.VISIBLE);
			ibtnSave.setImageResource(mivSave);
			ibtnSave.setOnClickListener(this);
		}
		if (isShowBack) {
			ibtnBack.setVisibility(View.VISIBLE);
			ibtnBack.setOnClickListener(this);
		}
	}

	public abstract void onClickSave(View v);

	public abstract void onClickBack(View v);

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ibtn_back:
			onClickBack(v);
			break;
		case R.id.ibtn_save:
		case R.id.tv_save:
			onClickSave(v);
			break;
		}
	}
}
